// pages/voucherSurvey/voucherSurvey.js
import questionnaireApi from "../../service/questionnaireApi";

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */

  data: {
    QOne: [
      { name: "1", value: "线下旗舰店", checked: ''},
      { name: "2", value: "线下工厂店", checked: '' },
      { name: "3", value: "网店", checked: '' }
    ],
    QTwo: [
      { name: "1", value: "耐克", checked: '' },
      { name: "2", value: "阿迪达斯", checked: '' },
      { name: "3", value: "安德玛", checked: '' },
      { name: "4", value: "VANS", checked: '' },
      { name: "5", value: "匡威", checked: '' },
      { name: "6", value: "斯凯奇", checked: '' },
      { name: "7", value: "亚瑟士", checked: '' },
      { name: "8", value: "鬼冢虎", checked: '' },
      { name: "9", value: "新百伦", checked: '' }
    ],
    QThree: [
      { name: "1", value: "2000及以上", checked: '' },
      { name: "2", value: "1000-1999", checked: '' },
      { name: "3", value: "500-999", checked: '' },
      { name: "4", value: "500以下", checked: '' }
    ],
    voucherOver: false,
    form: {
      question1: "",
      question2: "",
      question3: "",
      userSuggest: ""
    },
    cardPopup: false,
    couponInfo: {},
    picBaseUrl: app.globalData.picBaseUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.queryQuestionnaire()
  },

  // 获取答案
  queryQuestionnaire() {
    questionnaireApi.queryQuestionnaire().then(({ data }) => {
      if (data.errCode === app.globalData.codeConfig.errCode.SUCCESS_CODE) {
        this.setData({
          'form.userSuggest': data.data.userSuggest,
          voucherOver: true
          // cardPopup: true
        })
        var Q1 = data.data.question1.split(',')
        var Q2 = data.data.question2.split(',')
        var Q3 = data.data.question3.split(',')
        this.fn(Q1, this.data.QOne, 'QOne')
        this.fn(Q2, this.data.QTwo, 'QTwo')
        this.fn(Q3, this.data.QThree, 'QThree')
      } else if (data.errCode === '500007') { // 没有提交过问卷
        this.setData({
          cardPopup: false
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
  },
  fn (Q, QNum, _Qnum) {
    for (let i = 0; i < QNum.length; i++) {
      for (let j = 0; j < Q.length; j++) {
        if (QNum[i].name === Q[j]) {
          let question = _Qnum + `[${i}].checked`
          this.setData({
            [question]: 'true'
          })
        }            
      }
    }
  },
  // 第一题 
  checkboxChange1(e) {
    var val = e.detail.value
    this.setData({
      'form.question1': val.join(',')
    })
  },
  checkboxChange2(e) {
    var val = e.detail.value
    this.setData({
      'form.question2': val.join(',')
    })
  },
  radioChange(e) {
    var val = e.detail.value
    this.setData({
      'form.question3': val
    })
  },
  // 意见框
  onOpinion(e) {
    this.setData({
      'form.userSuggest': e.detail.value
    })
  },

  // 提交问券调查
  onQuestionnaire() {
    if (this.data.form.question1 === '' || this.data.form.question2 === '' || this.data.form.question3 === '') {
      wx.showToast({
        title: '请填写完问卷再提交',
        icon: 'none'
      })
      return false
    }
    var params = { ...this.data.form }
    // 获取奖励的优惠券
    questionnaireApi.questionnaire(params).then(res => {
      if (res.data.errCode === app.globalData.codeConfig.errCode.SUCCESS_CODE) {
        this.setData({
          voucherOver: true,
          cardPopup: true
        })
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 300
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {}
});
